#include "audioinfoinput.h"
#include <ClientImpl/Receive_handler.h>

AudioInfoInPut::AudioInfoInPut(OpenSl_Engine *pOpenSlEngine,Receive_handler *pHandler):m_pOpenSlEngine(pOpenSlEngine)
  ,m_handler(pHandler)
{
    recorderObject = 0;
    SLDataLocator_IODevice loc_dev = {SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT,
        SL_DEFAULTDEVICEID_AUDIOINPUT, NULL};
    SLDataSource audioSrc = {&loc_dev, NULL};

    SLDataLocator_AndroidSimpleBufferQueue loc_bq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
    SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, 1, SL_SAMPLINGRATE_8,
        SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
            SL_SPEAKER_FRONT_CENTER, SL_BYTEORDER_LITTLEENDIAN};
    SLDataSink audioSnk = {&loc_bq, &format_pcm};
    // 单声道 8000 采样  16位
    SLresult result;
    const SLInterfaceID id[1] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE};
    const SLboolean req[1] = {SL_BOOLEAN_TRUE};

    result = (*(pOpenSlEngine->engineEngine))->CreateAudioRecorder(pOpenSlEngine->engineEngine, &recorderObject, &audioSrc,&audioSnk, 1, id, req);
    if (SL_RESULT_SUCCESS != result) {
       return ;
    }

    //qDebug(QString("xxxxxx222222").toLocal8Bit().data());
    result = (*recorderObject)->Realize(recorderObject, SL_BOOLEAN_FALSE);
    if (SL_RESULT_SUCCESS != result) {
        return ;
    }

    //qDebug(QString("xxxxxx33333333").toLocal8Bit().data());
    result = (*recorderObject)->GetInterface(recorderObject, SL_IID_RECORD, &recorderRecord);

    //qDebug(QString("xxxxxx444444").toLocal8Bit().data());
    result = (*recorderObject)->GetInterface(recorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
          &recorderBufferQueue);

    result = (*recorderBufferQueue)->RegisterCallback(recorderBufferQueue, bqRecorderCallback, this);
    //qDebug(QString("xxxxxx5555555555").toLocal8Bit().data());
}


AudioInfoInPut::~AudioInfoInPut()
{
   (*recorderObject)->Destroy(recorderObject);
    recorderObject = 0;
}

void AudioInfoInPut::bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
{
    AudioInfoInPut *pContext = (AudioInfoInPut *)context;
    if(pContext)
        pContext->RecorderCallback(bq);
}

void AudioInfoInPut::RecorderCallback(SLAndroidSimpleBufferQueueItf bq)
{
    SLresult result;

    result = (*(bq))->Enqueue(bq, recorderBuffer,1024*sizeof(short));

    CCriticalAutoLock loAutoLock(oCriSection);
    m_handler->receive_handler((char*)&recorderBuffer,1024*sizeof(short));
//    m_InPut_buf.append(std::string((char*)&recorderBuffer,4096*sizeof(short)));//添加到后面 PCM 数据

//    while(m_InPut_buf.size()>=2048) // 16位的PCM  就是2K 大小
//    {
//        if(m_handler)
//        {
//            m_handler->receive_handler(const_cast <char*>(m_InPut_buf.data()),2048);

//            m_InPut_buf = std::string(m_InPut_buf.begin()+ 2048,m_InPut_buf.end());
//        }
//    }
}

void AudioInfoInPut::start()
{
    stop();

    SLresult result;

    {
        CCriticalAutoLock loAutoLock(oCriSection);
        result = (*recorderBufferQueue)->Enqueue(recorderBufferQueue, recorderBuffer,
                1024 * sizeof(short));
    }

    // 开始录音
    result = (*recorderRecord)->SetRecordState(recorderRecord, SL_RECORDSTATE_RECORDING);
}

void AudioInfoInPut::stop()
{
    SLresult  result;
    result = (*recorderRecord)->SetRecordState(recorderRecord, SL_RECORDSTATE_STOPPED);
    CCriticalAutoLock loAutoLock(oCriSection);
    result = (*recorderBufferQueue)->Clear(recorderBufferQueue);
}
//qint64 AudioInfoInPut::writeData(const char *data, qint64 len)
//{
//    //qDebug(QString("AudioInfoInPut::writeData size:").append(QString::number((long)len)).toLocal8Bit().data());
//    CCriticalAutoLock loAutoLock(oCriSection);

//    m_InPut_buf.append(std::string(data,len));//添加到后面 PCM 数据

//    while(m_InPut_buf.size()>=2048) // 16位的PCM  就是2K 大小
//    {
//        if(m_handler)
//        {
//            m_handler->receive_handler(const_cast <char*>(m_InPut_buf.data()),2048);

//            m_InPut_buf = std::string(m_InPut_buf.begin()+ 2048,m_InPut_buf.end());
//        }
//    }

//    return len;
//}

